package com.tarena.controller;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.tarena.entity.Booking;
import com.tarena.entity.Meettingroom;
import com.tarena.entity.User;
import com.tarena.entity.Meettingaddress;
import com.tarena.entity.Viewmodelbooking;
import com.tarena.service.BookingService;
import com.tarena.service.MeettingaddressService;
import com.tarena.service.MeettingroomService;
/**
 * liubiao
 */
@Controller
@RequestMapping("/booking")

public class BookingController {
	@Resource
	private BookingService bookingService;
	@Resource
	private MeettingroomService  meettingroomService;
	@Resource
	private MeettingaddressService  meettingaddressService;
	private PrintWriter out;
	
	//管理员添加(预定)
	@RequestMapping("/saveBooking.do")
	public void save(Booking booking, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		boolean save = bookingService.save(booking);
		out = res.getWriter();
		if (save) {
			out.write("1");
			out.flush();
			out.close();
		} else {
			out.write("0");
			out.flush();
			out.close();
		}
	}
	
	//删除
	@RequestMapping("/deleteBooking.do")
	public void delete(HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		System.out.println("delete");
		boolean delete = bookingService.delete(Integer.parseInt(req
				.getParameter("id")));
		out = res.getWriter();
		if (delete) {
			out.write("1");
			out.flush();
			out.close();
		} else {
			out.write("0");
			out.flush();
			out.close();
		}
	}
	
	//修改
	@RequestMapping("/updateBooking.do")
	public void update(Booking booking, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		out = res.getWriter();
		try {
			bookingService.update(booking);
			out.write("1");
			out.flush();
			out.close();
		} catch (Exception e) {
			out.write("0");
			out.flush();
			out.close();
		}
	}
	
	//查找所有
	@RequestMapping("/findBooking.do")
	public String findAll(Model mode) {
		List<Booking> list = bookingService.findAllBooking();
		mode.addAttribute("bookings", list);
		return "booking/booking_list";
	}
	
	//按ID查找
	@RequestMapping("/findById.do")
	public String findById(HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		System.out.println("findById");
		List<Booking> list = bookingService.findById(Integer.parseInt(req
				.getParameter("id")));
		req.setAttribute("bookings", list);
		return "booking/booking_list";
	}
	
	//按userId查找
	@RequestMapping("/findByUserId.do")
	public void findByUserId(HttpServletRequest req, HttpServletResponse res) throws Exception {
		System.out.println("aaaaaa");
		User user = (User)req.getSession().getAttribute("user_online");
		System.out.println(user.getId());
		List<Booking> list = bookingService.findByUserId(user.getId());
		
		System.out.println(list.get(0).getBookingDate());
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
		jsonMap.put("total", 1);//total键 存放总记录数，必须的  
        jsonMap.put("rows", list);//rows键 存放每页记录 list  

        JSONObject result = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject
        out = res.getWriter();
		out.write(result.toString());
		System.out.println(result.toString());
		out.flush();
		out.close();
	}
	
	//分页查找
	@RequestMapping("/findByPage.do")
	public void findByPage(Booking booking, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		System.out.println("findByPage");
		String page = req.getParameter("page");
		String rows = req.getParameter("rows");
		System.out.println(page);
		System.out.println(booking.getUserId());
		System.out.println(booking.getMeettingRoomId());
		List<Booking> list = null;
		
			if (null==booking.getUserId()&&null== booking.getMeettingRoomId()){
				System.out.println("000000000000000000000000");
				System.out.println("userId="+booking.getUserId());
				System.out.println("meettingRoomId="+booking.getMeettingRoomId());
				list = bookingService.findByPage(
						Integer.parseInt(rows) * (Integer.parseInt(page) - 1),
						Integer.parseInt(rows));
			}else if (null!=booking.getUserId()&&null== booking.getMeettingRoomId()){
				booking.setUserId(booking.getUserId());
				System.out.println("111111111111111111111111111");
				System.out.println("userId="+booking.getUserId());
				System.out.println("meettingRoomId="+booking.getMeettingRoomId());
				list = bookingService.findByPage(
					Integer.parseInt(rows) * (Integer.parseInt(page) - 1),
					Integer.parseInt(rows), booking);
			}else if (null==booking.getUserId()&&null!= booking.getMeettingRoomId()){
				booking.setMeettingRoomId(booking.getMeettingRoomId());
				System.out.println("222222222222222222222222222");
				System.out.println("userId="+booking.getUserId());
				System.out.println("meettingRoomId="+booking.getMeettingRoomId());
				list = bookingService.findByPage(
						Integer.parseInt(rows) * (Integer.parseInt(page) - 1),
						Integer.parseInt(rows), booking);
			}else {
				booking.setUserId(booking.getUserId());
				booking.setMeettingRoomId(booking.getMeettingRoomId());
				System.out.println("333333333333333333333333333");
				System.out.println("userId="+booking.getUserId());
				System.out.println("meettingRoomId="+booking.getMeettingRoomId());
				list = bookingService.findByPage(
						Integer.parseInt(rows) * (Integer.parseInt(page) - 1),
						Integer.parseInt(rows), booking);
			}
			
		System.out.println(list.toString());
		Map<String, Object> jsonMap = new HashMap<String, Object>();// 定义map
		jsonMap.put("total", bookingService.getSum());// total键 存放总记录数，必须的
		jsonMap.put("rows", list);// rows键 存放每页记录 list
		JSONObject result = JSONObject.fromObject(jsonMap);// 格式化result
															// 一定要是JSONObject
		out = res.getWriter();
		out.write(result.toString());
		System.out.println(result.toString());
		out.flush();
		out.close();
	}
	
	
	//userfindByPage.do
	@RequestMapping("/userfindByPage.do")
	public void userfindByPage(Booking booking, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		String page = req.getParameter("page");
		String rows = req.getParameter("rows");
		  
		System.out.println(page);
		System.out.println(booking.getUserId());
		System.out.println(booking.getMeettingRoomId());
		List<Booking> list = null;
		if (null != booking.getMeettingRoomId() || null != booking.getUserId()) {
			list = bookingService.findByPage(Integer.parseInt(rows)
					* (Integer.parseInt(page) - 1), Integer.parseInt(rows),
					booking);
			
		} else {
			list = bookingService.findByPage(Integer.parseInt(rows)
					* (Integer.parseInt(page) - 1), Integer.parseInt(rows));
		}
		List<Viewmodelbooking> returelist = new ArrayList<Viewmodelbooking>();
		for(Booking b : list){
			Viewmodelbooking vmb = new Viewmodelbooking();
			Meettingroom mr = meettingroomService.findById(b.getMeettingRoomId()).get(0);
			System.out.println(mr.getId());
			Meettingaddress findById2 = meettingaddressService.findById(mr.getMeettingAddressId()).get(0);
			System.out.println(findById2.getId());
			if (vmb != null) {
				returelist.add(vmb);
			}
			
		}
		Map<String, Object> jsonMap = new HashMap<String, Object>();// 定义map
		jsonMap.put("total", bookingService.getSum());// total键 存放总记录数，必须的
		jsonMap.put("rows", returelist);// rows键 存放每页记录 list
		JSONObject result = JSONObject.fromObject(jsonMap);// 格式化result
															// 一定要是JSONObject
		out = res.getWriter();
		out.write(result.toString());
		System.out.println(result.toString());
		out.flush();
		out.close();
	}
		
		
		@RequestMapping("/usersaveBooking.do")
		public void usersave( Viewmodelbooking viewmodelbooking, HttpServletRequest req,
				HttpServletResponse res) throws Exception {
			Booking booking = new Booking();
			booking.setUserId(viewmodelbooking.getViewuserid());
			booking.setBookingDate(viewmodelbooking.getViewdata());
			booking.setStartDate(viewmodelbooking.getViewstartDate());
			booking.setEndDate(viewmodelbooking.getViewendDate());
			booking.setProjector(viewmodelbooking.getProjector());
			
			booking.setPhoneEEE(viewmodelbooking.getId());
			booking.setMeettingRoomId(viewmodelbooking.getViewphoneEEE());
			boolean save = bookingService.save(booking);
			out = res.getWriter();
			out = res.getWriter();
			if (save) {
				out.write("1");
				out.flush();
				out.close();
			} else {
				out.write("0");
				out.flush();
				out.close();
			}
		}
}
